package com.yf.esms.common;


import lombok.Data;

import java.io.Serializable;


/**
 * @author 99698
 */
@Data
public class ResultComponent<T> implements Serializable {


    private static final long serialVersionUID = -6906880166392498887L;

    private Boolean success;
    private String msg;
    private String code;
    private T data;

    public ResultComponent() {
    }

    public ResultComponent(Boolean success, String code, String msg) {
        this.success = success;
        this.msg = msg;
        this.code = code;
    }

    public ResultComponent(Boolean success, String code) {
        this.success = success;
        this.code = code;
    }

    public ResultComponent(Boolean success, String msg, String code, T data) {
        this.success = success;
        this.msg = msg;
        this.code = code;
        this.data = data;
    }

    public ResultComponent(Boolean success, String code, T data) {
        this.success = success;
        this.code = code;
        this.data = data;
    }

    public ResultComponent(Boolean success, ResponseCode responseCode) {
        this.success = success;
        this.msg = responseCode.getMsg();
        this.code = responseCode.getCode();
    }

    public ResultComponent(Boolean success, ResponseCode responseCode, T data) {
        this.success = success;
        this.msg = responseCode.getMsg();
        this.code = responseCode.getCode();
        this.data = data;
    }

    public void setResultError(ResponseCode responseCode){
        this.data = null;
        this.code = responseCode.getCode();
        this.msg = responseCode.getMsg();
        this.success = false;
    }


    public void setResultError(String code, String msg){
        this.data = null;
        this.code = code;
        this.msg = msg;
        this.success = false;
    }



    public static <T> ResultComponent<T> ok(T data) {
        return new ResultComponent<>(true, ResponseCode.SUCCESS, data);
    }

    public static <T> ResultComponent<T> ok() {
        return new ResultComponent<>(true, ResponseCode.SUCCESS);
    }

    public static <T> ResultComponent<T> ok(String code, String msg, T data) {
        return new ResultComponent<>(true, code, msg, data);
    }

    public static <T> ResultComponent<T> ok(ResponseCode responseCode, T data) {
        return new ResultComponent<>(true, responseCode, data);
    }

    public static <T> ResultComponent<T> fail(T data) {
        return new ResultComponent<>(false, ResponseCode.FAILED, data);

    }

    public static <T> ResultComponent<T> fail() {
        return new ResultComponent<>(false, ResponseCode.FAILED);
    }

    public static <T> ResultComponent<T> fail(String code, String msg) {
        return new ResultComponent<>(false, code, msg);
    }

    public static <T> ResultComponent<T> fail(String code, String msg, T data) {
        return new ResultComponent<>(false, code, msg, data);
    }

    public static <T> ResultComponent<T> fail(ResponseCode responseCode, T data) {
        return new ResultComponent<>(false, responseCode, data);
    }

    public static <T> ResultComponent<T> fail(ResponseCode responseCode) {
        return new ResultComponent<>(false, responseCode);
    }

}
